<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\User\LockUser;
use App\Models\User;
use Dcat\Admin\Admin;
use Dcat\Admin\Exception\InvalidArgumentException;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    protected string $title = '用户管理';

    /**
     * @throws InvalidArgumentException
     */
    public function grid(): Grid
    {
        $model = User::ofAdmin()
            ->with(['wechat', 'application'])
            ->orderByDesc('id');

        return Grid::make($model, function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableEditButton();

            $grid->actions([
                new LockUser(),
            ]);

            $grid->quickSearch(['id', 'openid', 'nickname'])
                ->placeholder('用户ID/OpenID/昵称');

            if (Admin::user()->isAdministrator()) {
                $grid->column('application.name', '所属应用');
            }
            $grid->column('wechat.name', '所属微信');
            $grid->column('avatar', '头像')
                ->image('', 32);
            $grid->column('openid')
                ->copyable();
            $grid->column('nickname', '昵称')
                ->copyable();
            $grid->column('lock', '锁定')
                ->bool();
            $grid->column('created_at');
        });
    }
}
